package com.xishu.service.print;

import com.xishu.bo.printer.PrinterLine;
import com.xishu.client.PrinterClient;
import com.xishu.config.Config;
import com.xishu.entity.order.Order;
import com.xishu.entity.order.OrderItem;
import com.xishu.entity.shop.Item;
import com.xishu.entity.shop.Shop;
import com.xishu.entity.shop.TakeOutSetting;
import com.xishu.service.*;
import com.xishu.service.order.base.OrderService;
import com.xishu.util.Tools;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.xishu.config.Constant.SPECIAL_ITEM;

public class CommonPrintService implements Tools {
    private static Logger logger = LoggerFactory.getLogger(CommonPrintService.class);
    private static CommonPrintService instance = new CommonPrintService();
    private LangService langService = LangService.getInstance();
    private CommonService commonService = CommonService.getInstance();
    private PrinterClient printerClient = PrinterClient.getInstance();
    private OrderService orderService = OrderService.getInstance();
    private CompanyService companyService = CompanyService.getInstance();
    private PayTypeService payTypeService = PayTypeService.getInstance();

    public static final int LINE_LENGTH = 48;
    public static final int ONE_ITEM_LENGTH = LINE_LENGTH / 3;
    private Config config = Config.getInstance();


    private CommonPrintService() {
    }

    public static CommonPrintService getInstance() {
        return instance;
    }

    public String getPrintItemName(boolean zh, OrderItem orderItem) {
        String printItemName = langService.getOrderItemName(orderItem, zh);

        //特殊菜不用再查询
        if (equals(orderItem.getItemId(), SPECIAL_ITEM)) {
            return printItemName;
        }

        Item item = null;
        try {
            item = ItemService.getInstance().findItem(orderItem.getItemId());
        } catch (Exception e) {
            logger.error("e", e);
        }

        if (item != null && isNotEmpty(item.getPrintAlias())) {
            printItemName = getItemPrintName(item, zh);
        }
        return StringUtils.stripEnd(printItemName, null);
    }

    /**
     * 获取菜品的打印名称
     *
     * @param item
     * @param zh
     * @return
     */
    public String getItemPrintName(Item item, boolean zh) {
        if (zh) {
            if (isNotEmpty(item.getPrintAlias_zh())) {
                return StringUtils.stripEnd(item.getPrintAlias_zh(), null);
            } else if (isNotEmpty(item.getPrintAlias_en())) {
                return StringUtils.stripEnd(item.getPrintAlias_en(), null);
            } else if (isNotEmpty(item.getPrintAlias())) {
                return StringUtils.stripEnd(item.getPrintAlias(), null);
            } else if (isNotEmpty(item.getName_zh())) {
                return StringUtils.stripEnd(item.getName_zh(), null);
            } else if (isNotEmpty(item.getName_en())) {
                return StringUtils.stripEnd(item.getName_en(), null);
            } else {
                return StringUtils.stripEnd(item.getName(), null);
            }
        } else {
            if (isNotEmpty(item.getPrintAlias_en())) {
                return StringUtils.stripEnd(item.getPrintAlias_en(), null);
            } else if (isNotEmpty(item.getPrintAlias_zh())) {
                return StringUtils.stripEnd(item.getPrintAlias_zh(), null);
            } else if (isNotEmpty(item.getPrintAlias())) {
                return StringUtils.stripEnd(item.getPrintAlias(), null);
            } else if (isNotEmpty(item.getName_en())) {
                return StringUtils.stripEnd(item.getName_en(), null);
            } else if (isNotEmpty(item.getName_zh())) {
                return StringUtils.stripEnd(item.getName_zh(), null);
            } else {
                return StringUtils.stripEnd(item.getName(), null);
            }
        }
    }

    /**
     * 获取商铺地址
     *
     * @param shop
     * @param zh
     * @return
     */
    public List<PrinterLine> getShopAddressLine(Shop shop, Boolean zh) {
        if (Config.getInstance().isHkVersion()) {
            List<PrinterLine> lineList = new ArrayList<>();
            String shopAddress = langService.getShopAddress(shop, zh);
            List<String> shopAddressList = printerClient.getStringList(shopAddress, LINE_LENGTH - printerClient.getGbkLength(config.label_address(zh)), zh);

            //添加第一行
            lineList.add(printerClient.newLine(config.label_address(zh) + shopAddressList.get(0)));
            //添加剩下的行
            shopAddressList = shopAddressList.subList(1, shopAddressList.size());
            for (String address : shopAddressList) {
                lineList.add(printerClient.newLine(printerClient.generateSpace(printerClient.getGbkLength(config.label_address(zh))) + address));
            }

            return lineList;
        } else {
            //美国版本，第一行显示门牌号和街名，第二行显示剩余信息。 201 University Ave Palo Alto CA 94301
            List<PrinterLine> lineList = new ArrayList<>();

            //添加第一行
            lineList.add(printerClient.newLine(config.label_address(zh) + shop.getUsAddress()));
            //添加剩下的行
            List<String> addressList = Arrays.asList(shop.getUsCity(), ShopService.getInstance().getStateAbbreviation(shop.getUsState()), shop.getUsZip());

            int gbkLength = printerClient.getGbkLength(config.label_address(zh));

            lineList.add(printerClient.newLine(printerClient.generateSpace(gbkLength) + StringUtils.join(addressList, " ")));

            return lineList;
        }
    }

    /**
     * 获取打印名称，如果有数量大于2的，那么添加
     *
     * @param itemName
     * @param item
     * @return
     */
    public String getItemNameWithCount(String itemName, Item item) {
        if (getInt(item.getCountInOrder()) >= 2) {
            itemName = itemName + "*" + item.getCountInOrder();
            return itemName;
        }

        return itemName;
    }

    /**
     * 获取外卖标签
     *
     * @param order
     * @param zh
     * @return
     */
    public String getTakeoutLabel(Order order, Boolean zh, String defaultValue) {
        if (order == null) {
            return defaultValue;
        }

        if (order.getTakeoutSettingId() == null) {
            return defaultValue;
        }

        //如果有外卖设定，用外卖设定里面的中英文显示
        try {
            TakeOutSetting takeOutSetting = TakeOutSettingService.getInstance().findById(order.getTakeoutSettingId());
            if (takeOutSetting == null) {
                return defaultValue;
            }

            if (zh == null || zh) {
                return takeOutSetting.getName_zh();
            }

            return takeOutSetting.getName_en();
        } catch (Exception e) {
            logger.error("e", e);
            return defaultValue;
        }
    }
}
